<template>
  <div
      class="infinite-list"
      v-infinite-scroll="load"
      infinite-scroll-distance="10"
      :infinite-scroll-disabled="disabled"
  >
    <el-carousel :interval="4000" type="card" height="200px">
      <el-carousel-item v-for="item in 3" :key="item">
        <el-image :src="require('@/assets/poster/poster'+item+'.jpg')" fit="cover" style="height: 100%; min-width: 100%">
          <template #error>
            <div class="image-slot">
              <el-icon><picture /></el-icon>
            </div>
          </template>
        </el-image>
      </el-carousel-item>
    </el-carousel>
    <el-row :gutter="20">
      <el-empty :image-size="200" v-if="produceList.length===0" description="暂无商品信息" style="height: 66vh;width: 100%" />
      <el-col
          v-for="(item) in produceList"
          :key="item"
          :span="6"
      >
        <el-card :body-style="{ padding: '0px' }" shadow="hover" @click="gotoDetails(item.uuid)">
          <el-image :src="item.img" fit="cover" class="card-img">
            <template #error>
              <div class="image-slot">
                <el-icon><picture /></el-icon>
              </div>
            </template>
          </el-image>
          <div class="card-info">
            <span>品名：{{item.name}}</span>
            <span>售价：￥ {{item.price}}</span>
            <span>上架日期：{{item.time}}</span>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import {getCurrentInstance} from "vue";
import request from "@/http/request";
import router from "@/router";

export default {
  name: "FarmCropsHome",
  data() {
    return {
      posterList: [],
      loading: false,
      disabled: false,
      produceList: [],
      pageNum: 1,
      pageSize: 4,
      total: 0,
      pages: (-1),
    }
  },
  methods: {
    load() {
      if (this.loading === false) {
        this.loading = true;
        if (this.pages>0 && this.pageNum > this.pages) {
          this.$tipUtil.infoTip("已为您加载所有商品信息");
          this.disabled = true;
        } else {
          request({
            "url": "produce/getProduceList",
            "method": "get",
            params: {
              pageNum: this.pageNum,
              pageSize: this.pageSize,
            }
          }).then((response) => {
            if (response.code === 200) {
              const _this = this;
              this.pageNum++;
              this.total = response.data[0].total;
              this.pages = response.data[0].pages;
              response.data[0].data.map(function (item) {
                item.img = _this.staticResourcesUrl + item.img;
                _this.produceList.push(item);
              });
            } else if (response.code === 230) {
              this.$tipUtil.warningTip(response.msg);
            } else {
              this.$tipUtil.errTip(response.msg);
            }
            this.loading = false;
          })
        }
      }
    },
    gotoDetails(uuid) {
      router.push({
        name: "FarmCropsDetails",
        params: {
          uuid: uuid,
        }
      })
    },
  },
  setup() {
    const { appContext : { config: { globalProperties } } } = getCurrentInstance();
    const staticResourcesUrl = globalProperties.$staticResourcesUrl;
    return {
      staticResourcesUrl,
    }
  },
}
</script>

<style scoped>
.infinite-list {
  height: calc(100vh - 60px);
  margin: 0;
  padding: 20px 10px;
  overflow: auto;
  box-sizing: border-box;
}
.el-col{
  margin-bottom: 20px;
}
.el-col:last-child{
  margin-bottom: 0;
}
.card-img {
  height: 180px;
  min-width: 100%;
}
.card-info {
  height: 70px;
  padding: 7px 0;
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  align-items: center;
}
.card-info > span:nth-child(1){
  font-size: 16px;
  font-weight: 700;
  color: #53a8ff;
}
.card-info > span:nth-child(2){
  font-size: 14px;
  color: #ff6363;
}
.card-info > span:nth-child(3){
  font-size: 14px;
  color: #7a7a7a;
}
</style>